package uml.GUI;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import uml.controller.Controller;
import uml.eventsystem.IObserver;
import uml.eventsystem.events.UMLEventId;
import uml.layout.NoFlowLayout;
import uml.objects.AnchorPoint;
import uml.objects.ArrowEnd;
import uml.objects.ArrowLine;
import uml.objects.RelationshipArrow;
import uml.objects.UMLObject;
import uml.util.UMLConstants;
import uml.view.UMLContainerView;
import uml.view.factory.ContainerFactory;
import uml.view.factory.RelationshipArrowFactory;

public class MainWindow
		extends JFrame
		implements IObserver
{
	CommandRibbon m_menu;
	ControlPalatte m_buttonPalatte;
	DiagramPanel m_diagramPanel;
	private static final long serialVersionUID = 7939492728719590916L;
	private final String WINDOWTITLE = "A-Team UML Editor";
	/**
	 * Initializes the window and sets up the gui
	 * @param controller
	 */
	public MainWindow (Controller controller)
	{
		super ();
		initWindow (controller);
		initGui (controller);
	}
	/**
	 * Handles setting up the gui
	 * @param controller
	 */
	private void initGui (Controller controller)
	{
		Container contentPane = this.getContentPane ();
        contentPane.setBackground (UMLConstants.WINDOW_BACKGROUND);
        contentPane.setLayout (new MigLayout ("wrap 2, fillx", "[grow] []"));
		
		m_diagramPanel = new DiagramPanel();
		m_diagramPanel.setBackground(Color.magenta);
		contentPane.add (m_diagramPanel, "growx, growy");
		
		m_menu = new CommandRibbon (controller);
		this.setJMenuBar (m_menu);
		
		m_buttonPalatte = new ControlPalatte(controller);
		m_buttonPalatte.setBackground(Color.cyan);
		contentPane.add(m_buttonPalatte, "growy, width 100, east");
		
		this.validate ();
	}
	/**
	 * Adds an object to the <tt>DiagramPane</tt>
	 * @param object
	 */
	public void addObject(UMLObject object)
	{
		m_diagramPanel.add(object.getComponent ());
		m_diagramPanel.validate ();
	}
	/**
	 * Sets up the window
	 * @param controller
	 */
	private void initWindow (Controller controller)
	{
		setTitle (WINDOWTITLE);
		setDefaultCloseOperation (EXIT_ON_CLOSE);
		
		setSize (800, 600);
		setVisible (true);
	}
	
	@Override
	public void handleNotification (UMLEventId eventId)
	{
		if (eventId.equals (UMLEventId.APPLICATION_EXIT))
		{
			this.dispose ();
		}
	}
}
